package pl.agh.convexhulltutor.structures;

import java.awt.Color;
import java.awt.geom.Ellipse2D;

public class Point implements Comparable<Point> {
	public static final double POINT_SIZE = 8;
	public static final double EPSILON = 0.00001;
	public static final Color DEFAULT_COLOR = Color.BLUE;
	public static final Color VERTEX_COLOR = Color.ORANGE;
	public static final Color NON_CONVEX_HULL_COLOR = Color.RED;
	public static final Color CONVEX_HULL_COLOR = Color.GREEN;

	private double x;
	private double y;
	private Color previousColor;
	private Color color;

	public Point(double x, double y) {
		this(x, y, DEFAULT_COLOR);
	}

	public Point(double x, double y, Color color) {
		this.x = x;
		this.y = y;
		this.color = color;
		previousColor = color;
	}

	public Point(Point point) {
		x = point.x;
		y = point.y;
		color = point.color;
		previousColor = point.previousColor;
	}

	@Override
	public int compareTo(Point p) {
		return compareCoordinates(p, true);
	}

	public int compareCoordinates(Point p, boolean xMoreImportant) {
		int xDiffSign = Math.abs(x - p.x) < EPSILON ? 0 : x - p.x < 0 ? -1 : 1;
		int yDiffSign = Math.abs(y - p.y) < EPSILON ? 0 : y - p.y < 0 ? -1 : 1;
		if (xMoreImportant) {
			return xDiffSign != 0 ? xDiffSign : yDiffSign;
		} else {
			return yDiffSign != 0 ? yDiffSign : xDiffSign;
		}
	}

	public boolean isIdentical(Point p) {
		return compareTo(p) == 0 && color == p.color ? true : false;
	}

	public boolean isOnPoint(int clickCoordinateX, int clickCoordinateY) {
		if (clickCoordinateX > x - POINT_SIZE / 2.0
				&& clickCoordinateX < x + POINT_SIZE / 2.0
				&& clickCoordinateY > y - POINT_SIZE / 2.0
				&& clickCoordinateY < y + POINT_SIZE / 2.0) {
			return true;
		}
		return false;
	}

	public Ellipse2D getEllipse() {
		return new Ellipse2D.Double(x - POINT_SIZE / 2.0, y - POINT_SIZE / 2.0,
				POINT_SIZE, POINT_SIZE);
	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		previousColor = this.color;
		this.color = color;
	}

	public void setPreviousColor() {
		color = previousColor;
	}

	@Override
	public String toString() {
		return "(" + x + ", " + y + ")";
	}
}